var DirectionType;
(function (DirectionType) {
    DirectionType[DirectionType["left"] = 0] = "left";
    DirectionType[DirectionType["right"] = 1] = "right";
    DirectionType[DirectionType["up"] = 2] = "up";
    DirectionType[DirectionType["down"] = 3] = "down";
    DirectionType[DirectionType["stop"] = 4] = "stop";
})(DirectionType || (DirectionType = {}));
export class Snack {
    constructor(ele, _width = 600, _height = 600) {
        this.width = 600;
        this.height = 600;
        this.canvas = document.createElement("canvas");
        this.ctx = null;
        this.scope = 0;
        this.step = 15;
        this.snakeStruct = {
            body: [
                { x: Math.floor(this.width / 2), y: Math.floor(this.height / 2) }
            ],
            size: 1,
            direction: { x: DirectionType.left, y: DirectionType.up }
        };
        this.isEnd = false;
        this.master = [];
        this.lisFun = new Map();
        this.width = _width;
        this.height = _height;
        this.canvas.width = this.width;
        this.canvas.height = this.height;
        if (!ele.querySelector("canvas")) {
            ele.appendChild(this.canvas);
        }
    }
    start() {
        this.isEnd = false;
        this.scope = 0;
        // if (this.lisFun) {
        //   // this.lisFun(this.scope)
        // }
        this.snakeStruct = {
            body: [
                { x: Math.floor(this.width / 2), y: Math.floor(this.height / 2) },
            ],
            size: 1,
            direction: { x: DirectionType.stop, y: DirectionType.up }
        };
        document.addEventListener("keydown", (ev) => {
            this.lisKey(ev);
        });
        this.init();
    }
    lisKey(ev) {
        if (ev.code == "ArrowDown") {
            if (this.snakeStruct.direction.y == DirectionType.up) {
                return;
            }
            this.snakeStruct.direction.y = DirectionType.down;
            this.snakeStruct.direction.x = DirectionType.stop;
        }
        else if (ev.code == "ArrowUp") {
            if (this.snakeStruct.direction.y == DirectionType.down) {
                return;
            }
            this.snakeStruct.direction.y = DirectionType.up;
            this.snakeStruct.direction.x = DirectionType.stop;
        }
        else if (ev.code == "ArrowLeft") {
            if (this.snakeStruct.direction.x == DirectionType.right) {
                return;
            }
            this.snakeStruct.direction.x = DirectionType.left;
            this.snakeStruct.direction.y = DirectionType.stop;
        }
        else if (ev.code == "ArrowRight") {
            if (this.snakeStruct.direction.x == DirectionType.left) {
                return;
            }
            this.snakeStruct.direction.x = DirectionType.right;
            this.snakeStruct.direction.y = DirectionType.stop;
        }
    }
    init() {
        this.ctx = this.canvas.getContext("2d");
        if (this.ctx) {
            this.reRender();
            this.render();
        }
    }
    render() {
        // init
        this.drawSnack();
        this.renderTime = setInterval(() => {
            if (this.isEnd) {
                clearInterval(this.renderTime);
                console.log("结束游戏");
                this.updateEndGame();
                let lisFun = this.lisFun.get("game_over");
                if (lisFun) {
                    lisFun();
                }
            }
            else {
                this.updateGame();
            }
        }, 100);
    }
    updateMaster() {
        if (this.master.length == 0) {
            let x = Math.ceil(Math.random() * this.width - 15);
            let y = Math.ceil(Math.random() * this.height - 15);
            this.master.push({
                x: x, y: y
            });
        }
        this.master.filter(item => {
            let { x, y } = item;
            if (this.ctx) {
                this.ctx.fillStyle = 'rgb(251, 189, 204)';
                this.ctx.fillRect(x, y, this.step, this.step);
            }
        });
    }
    updateGame() {
        if (this.ctx) {
            this.updateSnack();
            this.reRender();
            this.updateMaster();
            this.drawSnack();
        }
    }
    reRender() {
        if (this.ctx) {
            this.ctx.fillStyle = 'rgb(0,0,0)';
            this.ctx.fillRect(0, 0, this.width, this.height);
        }
    }
    drawSnack() {
        if (this.ctx) {
            this.ctx.fillStyle = 'rgb(0,255,0)';
            this.snakeStruct.body.filter(item => {
                var _a;
                (_a = this.ctx) === null || _a === void 0 ? void 0 : _a.fillRect(item.x, item.y, this.step, this.step);
            });
        }
        if (this.judgeBox()) {
            this.master.length = 0;
            this.snakeStruct.body.push({
                x: this.snakeStruct.body[this.snakeStruct.body.length - 1].x + this.step,
                y: this.snakeStruct.body[this.snakeStruct.body.length - 1].y + this.step,
            });
            this.scope += 1;
            let lisFun = this.lisFun.get("scope");
            if (lisFun) {
                lisFun(this.scope);
            }
        }
    }
    // 判断是否相撞
    judgeBox() {
        if (this.master.length == 0) {
            return false;
        }
        let x1 = this.master[0].x;
        let x2 = this.master[0].x + this.step;
        let y1 = this.master[0].y;
        let y2 = this.master[0].y + this.step;
        let x3 = this.snakeStruct.body[0].x;
        let x4 = this.snakeStruct.body[0].x + this.step;
        let y3 = this.snakeStruct.body[0].y;
        let y4 = this.snakeStruct.body[0].y + this.step;
        if (Math.max(x1, x3) <= Math.min(x2, x4) && Math.max(y1, y3) <= Math.min(y2, y4)) {
            return true;
        }
        return false;
    }
    updateSnack() {
        // let item = this.snakeStruct.body[0]
        let _lastitem = { x: this.snakeStruct.body[0].x, y: this.snakeStruct.body[0].y };
        this.snakeStruct.body.filter((item, index) => {
            let _item = { x: item.x, y: item.y };
            if (index == 0) {
                if (this.snakeStruct.direction.x == DirectionType.left) {
                    item.x -= this.step;
                    this.snakeStruct.direction.y = DirectionType.stop;
                }
                else if (this.snakeStruct.direction.x == DirectionType.right) {
                    item.x += this.step;
                    this.snakeStruct.direction.y = DirectionType.stop;
                }
                else if (this.snakeStruct.direction.y == DirectionType.up) {
                    item.y -= this.step;
                    this.snakeStruct.direction.x = DirectionType.stop;
                }
                else if (this.snakeStruct.direction.y == DirectionType.down) {
                    item.y += this.step;
                    this.snakeStruct.direction.x = DirectionType.stop;
                }
            }
            else {
                item.x = _lastitem.x;
                item.y = _lastitem.y;
                _lastitem = _item;
            }
        });
        if (this.snakeStruct.body[0].x < 0 || this.snakeStruct.body[0].x > this.width || this.snakeStruct.body[0].y < 0
            || this.snakeStruct.body[0].y > this.height) {
            this.isEnd = true;
        }
    }
    updateEndGame() {
        if (this.ctx) {
            // background
            this.ctx.fillStyle = 'rgb(0,0,0)';
            this.ctx.fillRect(0, 0, this.width, this.height);
            this.ctx.fillStyle = 'rgb(255,255,255)';
            this.ctx.fillText("GAME  OVER", this.width / 2 - 50, this.height / 2);
        }
        document.removeEventListener("keydown", this.lisKey);
    }
    addListen(funStr, fun) {
        this.lisFun.set(funStr.toString(), fun);
    }
    dispose() {
        this.isEnd = true;
        document.removeEventListener("keydown", this.lisKey);
        this.lisFun.clear();
    }
}
